package com.bocai.yuweiwangluo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.View.RoundImageView;
import com.bocai.yuweiwangluo.bean.MatchListBean;
import com.bocai.yuweiwangluo.utils.ImageLoaderUtil;
import com.bocai.yuweiwangluo.utils.MyConst;
import com.bocai.yuweiwangluo.utils.TimeUtils;
import com.bocai.yuweiwangluo.utils.UrlData;


public class MatchListAdapter extends BaseAdapter
{
    private Context context;
    private MatchListBean matchListBean;
    public MatchListAdapter(Context context, MatchListBean matchListBean)
    {
        this.matchListBean = matchListBean;
        this.context=context;
    }
    public  void addData(MatchListBean bean){
        matchListBean.getData().addAll(bean.getData());
        this.notifyDataSetChanged();
    }
    @Override
    public int getCount()
    {
        return matchListBean.getData().size();
    }

    @Override
    public Object getItem(int position)
    {
        return matchListBean.getData().get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        try{
            ViewHolder viewHolder = null;
            if (convertView == null)
            {
                viewHolder = new ViewHolder();
                convertView= LayoutInflater.from(context).inflate(R.layout.item_match_list,null);
                viewHolder.ivSportType=(ImageView)convertView.findViewById(R.id.iv_yuezhan_sporttype);
                viewHolder.tvSportType=(TextView)convertView.findViewById(R.id.tv_yuezhan_sporttype);
                viewHolder.rivPhoto=(RoundImageView)convertView.findViewById(R.id.rid_yueezhan_photo);
                viewHolder.tvName=(TextView)convertView.findViewById(R.id.tv_yuezhan_name);
                viewHolder.tvPayType=(TextView)convertView.findViewById(R.id.tv_yuezhan_paytype);
                viewHolder.tvContent=(TextView)convertView.findViewById(R.id.tv_yuezhan_content);
                viewHolder.tvGym=(TextView)convertView.findViewById(R.id.tv_yuezhan_gym);
                viewHolder.tvMoney=(TextView)convertView.findViewById(R.id.tv_yuezhan_money);
                viewHolder.tvTime=(TextView)convertView.findViewById(R.id.tv_yuezhan_time);
                viewHolder.tvPersonCount=(TextView)convertView.findViewById(R.id.tv_yuezhan_personcount);
                convertView.setTag(viewHolder);
            } else
            {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            MatchListBean.Data data= matchListBean.getData().get(position);
            //设置数据（未完成）
            String type="";
            for(int i=0;i<MyConst.type.size();i++)
            {
                if(data.getType_id().equals(MyConst.type.get(i).getId()))
                {

                    type=MyConst.type.get(i).getTitle();
                }
            }
            viewHolder.tvSportType.setText(type);
            if("足球".equals(type))
            {
                viewHolder.ivSportType.setImageResource(R.mipmap.yuezhan_football);
            }else if("篮球".equals(type))
            {
                viewHolder.ivSportType.setImageResource(R.mipmap.yuezhan_basketball);
            }else if("羽毛球".equals(type))
            {
                viewHolder.ivSportType.setImageResource(R.mipmap.yuezhan_yu);
            }else if("网球".equals(type))
            {
                viewHolder.ivSportType.setImageResource(R.mipmap.yuezhan_wangqiu);
            }

            ImageLoaderUtil.loaderImage(UrlData.IMAGEURL + data.getIcon(), viewHolder.rivPhoto,R.mipmap.photo_default);
            viewHolder.tvName.setText(data.getNickname());
            if("1".equals(data.getMatch_type()))
            {
                viewHolder.tvPayType.setText("AA");
                viewHolder.tvPayType.setBackgroundResource(R.mipmap.yuezhan_aa);
            }else
            {

                viewHolder.tvPayType.setText("免费");
                viewHolder.tvPayType.setBackgroundResource(R.mipmap.yuezhan_mian);
            }
            viewHolder.tvContent.setText(data.getContent());
            viewHolder.tvGym.setText(data.getAddr());
            viewHolder.tvTime.setText(TimeUtils.getTime(Long.parseLong(data.getStime())));
            if("0".equals(data.getNum()))
            {
                viewHolder.tvPersonCount.setText("不限");
            }else
            {
                viewHolder.tvPersonCount.setText(data.getNums()+"/"+data.getNum());
            }
            viewHolder.tvMoney.setText(data.getPrice());

        }catch (Exception e)
        {
        }

        return convertView;
    }


    public class ViewHolder
    {
        ImageView ivSportType;
        TextView tvSportType;
        RoundImageView rivPhoto;
        TextView tvName;
        TextView tvPayType;
        TextView tvContent;
        TextView tvGym;
        TextView tvMoney;
        TextView tvTime;
        TextView tvPersonCount;
    }
}
